﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using OgilvyOne.UKNow.DALFactory;
using ECCIT;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Mobile;
using OgilvyOne.UKNow.Mobile.Model;
using OgilvyOne.UKNow.Mobile.Model.Events;
using ECCIT.Extension.Weibo_v2.Common;
using OgilvyOne.UKNow.Mobile.Utilities;
using OgilvyOne.UKNow.Configuration;

namespace OgilvyOne.UKNow.Mobile.APIs.Events
{
  public   class GetSpel:IHttpHandler
    {
        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                getspel(context);
            }
            catch (Exception)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-310", "", "获取失败"));
            }
            
        }
        private void getspel(HttpContext context)
        {
            int pagesize = 10;
            int currindex = 0;
            if (context.Request.Params["page_size"] != null)
            {
                pagesize = int.Parse(context.Request.Params["page_size"].ToString());
            }
            if (context.Request.Params["current_page"] != null)
            {
                if (int.Parse(context.Request.Params["current_page"].ToString()) > 0)
                {
                    currindex = int.Parse(context.Request.Params["current_page"].ToString()) - 1;
                }

            }
            PagingParameter pagpar = PagingParameterFactory.CreateInstance(true, currindex, pagesize, null);
            PagingList plst= EventsDALFactory.Create(2052).GetFeaturedModel(pagpar);
            ToWrite(context, plst, pagpar);

        }
        private void ToWrite(HttpContext htc, PagingList plst, PagingParameter pagingParams)
        {
            List<EventInfo> lelc = new List<EventInfo>();
            ModelList<EventInfo> elacdl = new ModelList<EventInfo>();
            elacdl.CurrentPageIndex = pagingParams.CurrentPageIndex + 1;
            elacdl.List = toList(plst, lelc);
            elacdl.TotalCount = plst.Capacity;
            elacdl.PageCount = (plst.Capacity % pagingParams.PageSize == 0) ? plst.Capacity / pagingParams.PageSize : plst.Capacity / pagingParams.PageSize + 1;

            string s = JsonSerializationHelper.ObjectToJson(elacdl).Replace("null", "\"\"");
            htc.Response.Write(ResultUtility.ReturnResultJson("0", s, "获取成功"));
        }

        private List<EventInfo> toList(PagingList plst, List<EventInfo> lelc)
        {
            foreach (EventsModel em in plst.List)
            {
                EventInfo elac = ModelConvertUtility.ToEventInfo(em, false);
                lelc.Add(elac);
            }
            return lelc;
        }
    }
}
